package com.logic.comment.exection;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.comment.exception.ServiceException;
import com.logic.comment.core.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.ConstraintViolationException;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(ServiceException.class)
    @ResponseBody
    public AjaxResult serviceException(ServiceException e) {
        String message = e.getMessage();
        log.error("服务异常", e);
        if (JSONUtil.isTypeJSON(message)) {
            JSONObject bean = JSONUtil.toBean(message, JSONObject.class);
            Object data = bean.getObj("data");
            String msg = bean.getStr("msg");
            return AjaxResult.fail(msg, data);
        }
        return AjaxResult.fail(message);
    }

    @ExceptionHandler(LoginException.class)
    @ResponseBody
    public AjaxResult LoginException(LoginException e) {
        log.error("LoginException异常已捕获", e);
        return new AjaxResult(-1000, e.getMessage());
    }

    @ExceptionHandler(PlannerException.class)
    @ResponseBody
    public AjaxResult PlannerException(PlannerException e) {
        log.error("配表异常已捕获", e);
        return new AjaxResult(-500, e.getMessage());
    }


    @ExceptionHandler(InfoTips.class)
    @ResponseBody
    public AjaxResult InfoException(InfoTips e) {
        String message = e.getMessage();
        log.error("消息已捕获{}", message);
        return AjaxResult.error(message);
    }

    @ExceptionHandler(JsonTipsException.class)
    @ResponseBody
    public AjaxResult JsonTipsException(JsonTipsException e) {
        String jsonMessage = e.getMessage();
        log.error("JsonTipsException{}", jsonMessage);
        return new AjaxResult(-20, "", jsonMessage);
    }

    @ExceptionHandler({MethodArgumentNotValidException.class})
    @ResponseBody
    public AjaxResult MethodArgumentNotValidException(MethodArgumentNotValidException e) {
        List<ObjectError> allErrors = e.getBindingResult().getAllErrors();
        String message = allErrors.stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining(";"));
        log.error("异常已捕获:{}", message, e);
        return AjaxResult.error("请求参数错误: " + message);
    }

    @ExceptionHandler({ConstraintViolationException.class})
    @ResponseBody
    public AjaxResult ConstraintViolationException(ConstraintViolationException e) {
        String message = e.getMessage();//.split(":")[1]
        log.error("异常已捕获", e);
        return AjaxResult.error("请求参数错误: " + message);
    }

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public AjaxResult exception(Exception e) {
        log.error("异常已捕获");
        e.printStackTrace();
        return AjaxResult.error("服务器异常");
    }

    @ExceptionHandler(IndexOutOfBoundsException.class)
    @ResponseBody
    public AjaxResult duplicateKeyException(IndexOutOfBoundsException e) {
        log.error("异常已捕获");
        e.printStackTrace();
        return AjaxResult.error("数组越界");
    }

    @ExceptionHandler(HttpMessageNotReadableException.class)
    @ResponseBody
    public AjaxResult HttpMessageNotReadableException(HttpMessageNotReadableException e) {
        log.error("异常已捕获");
        e.printStackTrace();
        return AjaxResult.error("http消息不可读");
    }

    // SerializationException
    // 序列化异常
}
